/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.EstablishmentCommunicateBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.EstablishmentCommunicateDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.StafferDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Accessory;
import com.minierp.model.EstCommReceiver;
import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentCommunicate;
import com.minierp.model.Message;
import com.minierp.model.MessageReceiver;
import com.minierp.model.Staffer;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class EstablishmentCommunicateFormCtr extends SimpleFormController {
	private EstablishmentCommunicateDao establishmentCommunicateDao;
	private StafferDao stafferDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sfHour = new SimpleDateFormat("HH");
		SimpleDateFormat sfMinute = new SimpleDateFormat("mm");
		EstablishmentCommunicateBean form = new EstablishmentCommunicateBean();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		EstablishmentCommunicate ec;
		if (id != null && !id.equals("")) {
			ec = (EstablishmentCommunicate) this.establishmentCommunicateDao
					.get(EstablishmentCommunicate.class, Long.parseLong(id));
			if (ec != null) {
				if (ec.getEstId() != 0L) {
					Establishment clientId1 = (Establishment) this.establishmentCommunicateDao
							.get(Establishment.class, ec.getEstId());
					if (clientId1 != null) {
						form.setEstName(clientId1.getEstName());
					}
				}

				if (ec.getParentId() != 0L) {
					EstablishmentCommunicate clientId2 = (EstablishmentCommunicate) this.establishmentCommunicateDao
							.get(EstablishmentCommunicate.class,
									ec.getParentId());
					if (clientId2 != null) {
						form.setParentTitle(clientId2.getCreaterName() + "："
								+ clientId2.getTitle());
					}
				}

				form.setChildrenList(this.establishmentCommunicateDao
						.getList(ec.getId()));
				if (ec.getSmsRemindDate() != null) {
					form.setSrDateStr(sf.format(ec.getSmsRemindDate()));
					form.setSrDateHour(sfHour.format(ec.getSmsRemindDate()));
					form.setSrDateMinute(sfMinute.format(ec.getSmsRemindDate()));
					form.setOldSrDate(form.getSrDateStr() + " "
							+ form.getSrDateHour() + ":"
							+ form.getSrDateMinute());
				}

				if (ec.getCommType().equals("LOG")) {
					if (ub.getUser().getStafferId() == ec.getCreaterId()
							&& sf.format(new Date()).equals(
									sf.format(ec.getCreateTime()))) {
						form.setCanEdit("Y");
					} else {
						form.setCanEdit("N");
					}
				} else if (ub.getUser().getStafferId() == ec.getCreaterId()
						&& ec.getCompleted().equals("N")) {
					form.setCanEdit("Y");
				} else {
					form.setCanEdit("N");
				}

				if (ec.getAttachFile() != null
						&& !ec.getAttachFile().equals("")) {
					int clientId3 = ec.getAttachFile().lastIndexOf("/");
					form.setAttachFileName(ec.getAttachFile().substring(
							clientId3 + 1));
				}
			}
		} else {
			ec = new EstablishmentCommunicate();
			String clientId = request.getParameter("client_id");
			if (clientId != null && !clientId.equals("")) {
				ec.setEstId(Long.parseLong(clientId));
				Establishment parentId = (Establishment) this.establishmentCommunicateDao
						.get(Establishment.class, Long.parseLong(clientId));
				if (parentId != null) {
					form.setEstName(parentId.getEstName());
				}
			}

			String parentId1 = request.getParameter("parent_id");
			if (parentId1 != null && !parentId1.equals("")) {
				ec.setParentId(Long.parseLong(parentId1));
				EstablishmentCommunicate tmpCode = (EstablishmentCommunicate) this.establishmentCommunicateDao
						.get(EstablishmentCommunicate.class,
								Long.parseLong(parentId1));
				if (tmpCode != null) {
					form.setParentTitle(tmpCode.getCreaterName() + "："
							+ tmpCode.getTitle());
					if (tmpCode.getEstId() != 0L) {
						ec.setEstId(tmpCode.getEstId());
						Establishment c = (Establishment) this.establishmentCommunicateDao
								.get(Establishment.class, ec.getEstId());
						if (c != null) {
							form.setEstName(c.getEstName());
						}
					}
				}
			}

			ec.setCreaterId(ub.getStaffer().getId());
			ec.setCreaterName(ub.getStaffer().getStafferName());
			String tmpCode1 = ub.getUser().getId() + "_"
					+ System.currentTimeMillis();
			form.setTmpCode(tmpCode1);
			form.setCanEdit("Y");
			Calendar c1 = Calendar.getInstance();
			Date now = new Date();
			c1.setTime(now);
			c1.add(10, 1);
			form.setSrDateStr(sf.format(c1.getTime()));
			form.setSrDateHour(sfHour.format(c1.getTime()));
			form.setSrDateMinute(sfMinute.format(c1.getTime()));
		}

		form.setCommunicateTimeStr(sf.format(ec.getCommunicateTime()));
		form.setCommunicateTimeHour(sfHour.format(ec.getCommunicateTime()));
		form.setCommunicateTimeMinute(sfMinute.format(ec.getCommunicateTime()));
		form.setCommunicate(ec);
		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		EstablishmentCommunicateBean bean = (EstablishmentCommunicateBean) command;
		EstablishmentCommunicate obj = bean.getCommunicate();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("attach_file");
		String alertMsg;
		String continueFlag;
		String alertType;
		if (multipartFile != null && !multipartFile.isEmpty()) {
			alertMsg = multipartFile.getOriginalFilename();
			String mv = EncCommonHelper.getExtendName(alertMsg);
			continueFlag = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			alertType = this.getServletContext().getRealPath(continueFlag);
			String fileName = ub.getUser().getId() + "_"
					+ System.currentTimeMillis() + "." + mv;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(alertType
					+ System.getProperty("file.separator") + fileName));
			obj.setAttachFile(continueFlag + fileName);
		}

		alertMsg = "";
		if (id != null && !id.equals("")) {
			if (bean.getCanEdit().equals("Y")) {
				this.establishmentCommunicateDao.update(obj);
				alertMsg = "保存成功！";
			} else {
				alertMsg = "对不起，当前记录不允许修改！";
			}
		} else {
			if (obj.getParentId() != 0L) {
				EstablishmentCommunicate mv1 = (EstablishmentCommunicate) this.establishmentCommunicateDao
						.get(EstablishmentCommunicate.class, obj.getParentId());
				Staffer continueFlag1 = (Staffer) this.stafferDao.get(
						Staffer.class, mv1.getCreaterId());
				this.getMessage(continueFlag1, obj, (List) null);
			}

			obj.setId(this.idGenerator.getId(EstablishmentCommunicate.class
					.getName()));
			this.establishmentCommunicateDao.save(obj);
			if (obj.getCommType().equals("PLAN") && obj.getForAll().equals("N")) {
				this.establishmentCommunicateDao.updateReceiver(
						bean.getTmpCode(), obj.getId());
				List mv2 = this.establishmentCommunicateDao.getReceiverList(
						bean.getTmpCode(), obj.getId());
				if (mv2 != null && mv2.size() > 0) {
					EstCommReceiver continueFlag2 = (EstCommReceiver) mv2
							.get(0);
					Staffer alertType1 = (Staffer) this.stafferDao.get(
							Staffer.class, continueFlag2.getReceiverId());
					this.getMessage(alertType1, obj, mv2);
				}
			}

			alertMsg = "增加成功！";
		}

		if (obj.getEstId() != 0L && bean.getCanEdit().equals("Y")) {
			Establishment mv3 = (Establishment) this.establishmentCommunicateDao
					.get(Establishment.class, obj.getEstId());
			if (mv3 != null
					&& (mv3.getCommTime() == null || mv3.getCommTime().before(
							obj.getCommunicateTime()))) {
				mv3.setCommTime(obj.getCommunicateTime());
				this.establishmentCommunicateDao.update(mv3);
			}
		}

		continueFlag = request.getParameter("continue_flag");
		alertType = request.getParameter("alert_type");
		ModelAndView mv4;
		if (continueFlag != null && continueFlag.equals("1")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv4 = new ModelAndView(
					"redirect:/base/EstablishmentCommunicateNew.ihtm?alert_type="
							+ alertType + "&client_id=" + obj.getEstId());
		} else {
			mv4 = new ModelAndView(this.getSuccessView());
			mv4.addObject("returnValue", String.valueOf(obj.getId()));
			if (alertType == null || !alertType.equals("NONE")) {
				EncCommonHelper.setAlertMessage(request, alertMsg);
			}
		}

		mv4.addObject("alertType", alertType);
		return mv4;
	}

	public void getMessage(Staffer s, EstablishmentCommunicate obj, List<?> list) {
		String staName;
		if (s != null) {
			staName = s.getStafferName();
		} else {
			staName = "已删除";
		}

		Message message = new Message();
		message.setId(this.idGenerator.getId(Message.class.getName()));
		message.setTitle(obj.getTitle());
		message.setContent(obj.getContent());
		message.setSenderId(obj.getCreaterId());
		message.setSenderName(obj.getCreaterName());
		message.setReceiverName(staName);
		if (list != null && list.size() > 0) {
			message.setReceiverName(staName + "...");
		}

		this.establishmentCommunicateDao.save(message);
		if (list != null && list.size() > 0) {
			for (int arg8 = 0; arg8 < list.size(); ++arg8) {
				Staffer accessory;
				if (obj.getForAll().equals("Y")) {
					accessory = (Staffer) list.get(arg8);
				} else {
					EstCommReceiver sourceName = (EstCommReceiver) list
							.get(arg8);
					accessory = (Staffer) this.stafferDao.get(Staffer.class,
							sourceName.getReceiverId());
				}

				MessageReceiver arg11 = new MessageReceiver();
				arg11.setId(this.idGenerator.getId(MessageReceiver.class
						.getName()));
				arg11.setMessageId(message.getId());
				arg11.setReceiverId(accessory.getId());
				arg11.setReceiverName(accessory.getStafferName());
				this.establishmentCommunicateDao.save(arg11);
			}
		} else if (s != null) {
			MessageReceiver file = new MessageReceiver();
			file.setId(this.idGenerator.getId(MessageReceiver.class.getName()));
			file.setMessageId(message.getId());
			file.setReceiverId(s.getId());
			file.setReceiverName(s.getStafferName());
			this.establishmentCommunicateDao.save(file);
		}

		String arg9 = obj.getAttachFile();
		if (arg9 != null && !arg9.equals("")) {
			Accessory arg10 = new Accessory();
			String arg12 = arg9.substring(arg9.lastIndexOf("/") + 1);
			arg10.setId(this.idGenerator.getId(Accessory.class.getName()));
			arg10.setAgentId(message.getId());
			arg10.setAgentType("MESSAGE");
			arg10.setFileName(arg9);
			arg10.setCreaterId(obj.getCreaterId());
			arg10.setCreaterName(obj.getCreaterName());
			arg10.setSourceName(arg12);
			this.establishmentCommunicateDao.save(arg10);
		}

	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public EstablishmentCommunicateDao getEstablishmentCommunicateDao() {
		return this.establishmentCommunicateDao;
	}

	public void setEstablishmentCommunicateDao(
			EstablishmentCommunicateDao establishmentCommunicateDao) {
		this.establishmentCommunicateDao = establishmentCommunicateDao;
	}

	public StafferDao getStafferDao() {
		return this.stafferDao;
	}

	public void setStafferDao(StafferDao stafferDao) {
		this.stafferDao = stafferDao;
	}
}